package com.ruoyi.web.controller.construction.supertube;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductInfo;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.service.zhaungxiuservice.shopService.GroupService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.SupplierInfoService;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@CrossOrigin
@RequestMapping("/group/booking")
public class SuperGroupController extends BaseController {

    @Autowired
    private GroupService groupService;
    @Autowired(required = false)
    private TokenService tokenService;
    @Autowired
    private SupplierInfoService supplierInfoService;



    /**
     * 查询商品信息列表
     */
//    @PreAuthorize("@ss.hasPermi('group:booking:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        productInfo.setUserId(userId);
        List<ProductInfo> list = groupService.selectGroupInfoList(productInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('group:booking:export')")
    @Log(title = "拼团商品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductInfo productInfo)
    {
        List<ProductInfo> list = groupService.selectGroupInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('group:booking:query')")
    @GetMapping(value = "/getInfo/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Integer productId)
    {
        return AjaxResult.success(groupService.selectGroupInfoById(productId));
    }

    @GetMapping(value = "/findAllUserId")
    public List<SupplierInfo> findAllUserId(SupplierInfo supplierInfo){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long customerId = loginUser.getUser().getUserId();
//        supplierInfo.setCustomerId(customerId);
        List<SupplierInfo> allUserId = supplierInfoService.findAllUserId(supplierInfo);
        return allUserId;
    }

    @GetMapping(value = "/findAllStore")
    public List<SupplierInfo> findAllStore(){
        List<SupplierInfo> allUserId = supplierInfoService.findAllStore();
        return allUserId;
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('group:booking:add')")
    @Log(title = "拼团商品信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ProductInfo productInfo)
    {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        productInfo.setProductCore(num);
        productInfo.setAuditStatus("0");
        productInfo.setProductionDate(new Date());
        productInfo.setIndate(new Date());
        productInfo.setBrandId(666);
//        productInfo.setSupplierId(666);
        productInfo.setCategoryId(666);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        productInfo.setUserId(userId);

        productInfo.setGroupType("2");
        productInfo.setModifiedTime(new Date());
        return toAjax(groupService.insertGroupInfo(productInfo));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('group:booking:edit')")
    @Log(title = "拼团商品信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody ProductInfo productInfo)
    {
        return toAjax(groupService.updateGroupInfo(productInfo));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('group:booking:remove')")
    @Log(title = "拼团商品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{productIds}")
    public AjaxResult remove(@PathVariable Integer[] productIds)
    {
        return toAjax(groupService.deleteGroupByIds(productIds));
    }


}
